//
//  BKSCBuyCarTool.m
//  bestkeep
//
//  Created by 孙好运 on 2016/11/10.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKSCBuyCarTool.h"
#import "BKCMGoodsDetailModel.h"
#import "Mediator+BKMy.h"

@implementation BKSCBuyCarTool

#pragma mark - 购物车提示界面
/**
 *  购物车 提示界面
 *
 *  @param view        传入的View；
 *  @param emptyText   详情label 上内容；
 *  @param buttonTitle 按钮上的标题；
 *
 */
- (void)showEmptyMsgViewTo:(UIView *)view
                     emptyText:(NSString *)emptyText
                   buttonTitle:(NSString *)buttonTitle{
    
    UIImageView *logiImageView = [[UIImageView alloc] init];
    UIButton *retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [retryButton addTarget:self action:@selector(retryButtonMao1:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *infoLabel = [[UILabel alloc] init];
    [view addSubview:logiImageView];
    [view addSubview:retryButton];
    [view addSubview:infoLabel];
    logiImageView.tag = 17776;
    retryButton.tag = 17777;
    infoLabel.tag = 17778;
    infoLabel.font = [UIFont systemFontOfSize:15];
    infoLabel.textColor = RGB(184, 184, 184);
    if ([buttonTitle isEqualToString:@"去购物"]) {
        
        logiImageView.image = [UIImage imageNamed:@"BKSCCrossBorder-1"];
        logiImageView.backgroundColor = [UIColor clearColor];
        
        logiImageView.hidden = NO;
        [logiImageView setContentMode:UIViewContentModeScaleAspectFit];
        [logiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(view.mas_top).offset(28);
            make.left.mas_equalTo(view.mas_left).offset(96);
            make.centerX.mas_equalTo(view.mas_centerX);
            make.width.mas_equalTo(logiImageView.mas_height);
        }];
        //详情label
        infoLabel.text = emptyText;
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(logiImageView.mas_bottom).mas_offset(10);
            make.centerX.mas_equalTo(view.mas_centerX);
            make.height.mas_equalTo(15);
        }];
        //按钮
        [retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(view.mas_centerX);
            make.top.mas_equalTo(infoLabel.mas_bottom).mas_equalTo(13);
            make.width.mas_equalTo(90);
            make.height.mas_equalTo(26);
        }];
        
    }else {
        logiImageView.hidden = YES;
        
        //详情label
        infoLabel.text = emptyText;
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(retryButton.mas_top).mas_offset(-30);
            make.centerY.mas_equalTo(view.mas_centerY).mas_offset(-30);
            make.centerX.mas_equalTo(view.mas_centerX);
            make.height.mas_equalTo(30);
        }];
        
        //按钮
        [retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(view.mas_centerX);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(30);
        }];
        
    }
    
    retryButton.backgroundColor = [UIColor clearColor];
    [retryButton setTitleColor:RGB(184, 184, 184) forState:(UIControlStateNormal)];
    retryButton.bounds = CGRectMake(0, 0, 100, 30);
    
    [retryButton setTitle:buttonTitle forState:UIControlStateNormal];
    [retryButton setTitleColor:RGB(27, 188, 155) forState:UIControlStateNormal];
    retryButton.layer.borderColor = RGB(27, 188, 155).CGColor;
    retryButton.layer.borderWidth = 1.0f;
    retryButton.titleLabel.font = [UIFont systemFontOfSize:18];
    retryButton.layer.cornerRadius = 5;
    retryButton.layer.masksToBounds = YES;
    
}

- (void)retryButtonMao1:(UIButton *)sender {
    
    self.retryBlock(sender);
}


+ (UIView *)getViewWhenDataEndLoading {
    
    
    
    UIView *recommentFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 46)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 46)];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    imageView.image = [UIImage imageNamed:@"BKCMBuyCar_NoData"];
    [recommentFooterView addSubview:imageView];
    return recommentFooterView;
}

+ (void)updataBageIntage:(NSInteger)count bottomView:(UIView *)tabBarView {
    
    UITabBar *tabBar = (UITabBar *)tabBarView;
    if (count == 0) {
        [[tabBar.items objectAtIndex:2] setBadgeValue:nil];
    }else if (count < 100){
        [[tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"%ld",(long)count]];
    }else {
        [[tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"99+"]];
    }

}

/**
 *  创建推荐列表的头view
 *
 *  @return view 显示为你推荐；
 */
+ (UIView *)initializationRecommendFirstSectionView {
    
    UIView *recommendView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 40)];
    recommendView.backgroundColor = [UIColor whiteColor];
//    BUYCARGRAYCOLOR;
    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 83, 15)];
    [recommendView addSubview:centerView];
    centerView.center = CGPointMake(recommendView.center.x, recommendView.center.y);
    centerView.backgroundColor = [UIColor clearColor];
    
    UILabel *recommendLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 63, 15)];
    recommendLabel.text = BKSCLocalizedString(@"BKSCControllerRecommendTitle");
    recommendLabel.font = [UIFont systemFontOfSize:15];
    recommendLabel.textColor = HexColor(0x404040);
    [centerView addSubview:recommendLabel];
    [recommendLabel setTextAlignment:NSTextAlignmentCenter];
    
    //星星图片
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BKSCIcon_recommend"]];
    leftImageView.frame = CGRectMake(0, 0, 15, 15);
    [centerView addSubview:leftImageView];
    
    //底部线
    UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40.f, ScreenWidth, 0.5f)];
    [recommendView addSubview:lineImageView];
    lineImageView.backgroundColor = HexColor(0xDADADA);
    
//    CGFloat viewWidth = IPHONE_WIDTH * 277.0f / 750.0f;
//    CGFloat rightViewX = 453.0f / 750.0f * IPHONE_WIDTH ;
//    //用UIView创建两条线
//    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(10, 18.5f, viewWidth, 0.50f)];
//    leftView.backgroundColor = HexColor(0x979797);
//    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(rightViewX, 18.5f, viewWidth, 0.50f)];
//    rightView.backgroundColor = HexColor(0x979797);
//    
//    [recommendView addSubview:leftView];
//    [recommendView addSubview:rightView];
    
    return recommendView;
}


//购物车选中数据存储路径
+ (NSString *)selectedInfoDocumentPath:(NSString *)userInfo {
    
    NSString *private = [BKSCBuyCarTool documentsPath];
    private = [private stringByAppendingPathComponent:[NSString stringWithFormat:@"selectedInfo%@.plist",userInfo]];
    return private;
}

//沙河 document 路径；
+(NSString *)documentsPath {
    
    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [documentsPath objectAtIndex:0];
}


//将一个字典写入文件
+ (BOOL)writeBuyCarSelectDictionary:(NSMutableDictionary *)dic {
    
    NSDictionary *dictionary = [[Mediator sharedInstance]BKMyUserInfo];
    
    NSString *filePath = [BKSCBuyCarTool selectedInfoDocumentPath:dictionary[@"BKMyUserInfoGetUserIdKey"]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    if (!dic) {
        return NO;
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    return  [fileManager createFileAtPath:filePath contents:data attributes:nil];
}

//从文件中读取字典
+ (NSMutableDictionary *)readBuyCarSelectDictionaryWithFilePath {
    
    NSDictionary *dic = [[Mediator sharedInstance]BKMyUserInfo];
    NSString *filePath = [BKSCBuyCarTool selectedInfoDocumentPath:dic[@"BKMyUserInfoGetUserIdKey"]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData *data = [fileManager contentsAtPath:filePath];
        if (data == nil) {
            return [NSMutableDictionary dictionary];
        }
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        return dic;
    } else {
        
        return [NSMutableDictionary dictionary];
    }
    
}

+ (BOOL)getAllShopLoss:(NSMutableArray *)allArray {
    
    for (BKCMRecommendGoodsModel *section in allArray) {
        for (BKCMGoodsDetailModel *shopCell in section.shopList) {
            if (shopCell.shopcart_flag) {  //有未失效的进入
                return NO;
            }
        }
    }
    //全已失效；
    return YES;
}

/**
 *  判断section是否是选中状态
 *
 *  @param section                   记录状态的数据源
 *  @param mutableSelectedDictionary 盛放唯一标示符的字典
 *  @param leftButton                编辑按钮
 *
 *  @return 否是选中状态
 */
+(BOOL)getSectionSelectedStatu:(BKCMRecommendGoodsModel *)section mutableSelectedDictionary:(NSMutableDictionary *)mutableSelectedDictionary leftButton:(UIButton *)leftButton{
    if (section.shopList.count) {
        int i = 0;
        
        for (BKCMGoodsDetailModel * model in section.shopList) {
            
            for (NSString *record_id in [mutableSelectedDictionary allKeys]) {
                
                if ([model.record_id isEqualToString:record_id]) {
                    i++;
                }
            }
            
        }
        int j = 0;  //记录失效商品个数；
        for (BKCMGoodsDetailModel * model in section.shopList) {
            
            if (!model.shopcart_flag && !leftButton.selected) {
                j++;
            }
        }
        
        if (i == section.shopList.count - j) {
            return YES;
        }
    }
    return NO;
}

/**
 *  处理选择的事件
 *
 *  @param dataSource                盛放数据的大数组
 *  @param mutableSelectedDictionary 盛放唯一标示符的字典
 *  @param leftButton                编辑按钮
 *
 *  @return 选中的数组
 */
+(NSMutableArray *)getSelectedOrderArray:(NSMutableArray *)dataSource mutableSelectedDictionary:(NSMutableDictionary *)mutableSelectedDictionary leftButton:(UIButton *)leftButton{
    NSMutableArray  * selectedOrderArray = [[NSMutableArray alloc] init];
    
    [dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSArray * sectionArray = [(BKCMRecommendGoodsModel *)obj shopList];
        for (BKCMGoodsDetailModel * tempModel in sectionArray) {
            if (tempModel.isCellBtnSelect) {
                
                if (!tempModel.shopcart_flag && !leftButton.selected) {
                    tempModel.isCellBtnSelect = NO;
                    [mutableSelectedDictionary removeObjectForKey:tempModel.record_id];
                    continue;
                }else if (!tempModel.shopcart_flag) {
                    
                    [selectedOrderArray addObject:tempModel];
                    [mutableSelectedDictionary setValue:tempModel.record_id forKey:tempModel.record_id];
                    continue;
                }else {
                    
                    [selectedOrderArray addObject:tempModel];
                    
                    [mutableSelectedDictionary setValue:tempModel.record_id forKey:tempModel.record_id];
                }
            }
        }
    }];
    return selectedOrderArray;
}


@end
